package dd.prog2.exercises.set10;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class LargeFileFinder {

	public static void main(String[] args) {
		FileProcessor fp = new FileProcessor();
		FlexibleFileTreeWalker mytw = new FlexibleFileTreeWalker(fp);

		try {
			mytw.walk(".");
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("smth wrong");
		}
		fp.sort();
	}

	static class FileProcessor implements FlexibleFileTreeWalker.FileProcessor {

		private Map <String, Long> file_list = new HashMap<String, Long>();

		public FileProcessor() {}
		
		@Override
		public void process(File file) throws IOException {
//			System.out.printf("%6d %-100s%n", file.length(), file.getCanonicalPath());
			file_list.put(file.getCanonicalPath(),file.length());

		}
		
		@SuppressWarnings({ "all" })
		public void sort() {
			List sorted_list = new LinkedList(file_list.entrySet());
			// sort with Comparator
			Collections.sort(sorted_list, 
				new Comparator() {
				public int compare(Object o1, Object o2) {
					return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
				}
			});

			for (Iterator it = sorted_list.iterator(); it.hasNext();) {
				Map.Entry entry = (Map.Entry) it.next();
				System.out.printf("%6d %-100s%n",entry.getValue(), entry.getKey());
			}
		}

	}
}
